<?php

namespace App\HttpController\Api;

use App\Think\Db;
use App\Model\Trade as TradeModel;
use App\Model\Bonus as BonusLib;

/**
 * 回调处理Notify
 */
class Notify extends ApiBase
{
    protected $noNeedLogin = ['*'];

    /**
     * 余额充值回调
     * @param string $order_sn 订单ID
     */
    public function recharge(){
        // $payment = $this->input("payment");
        $payment = $this->request->param()['payment'];

        log_to($_REQUEST, "tradeRechargeCallback-{$payment}", '余额充值回调');
        log_to($_POST, "tradeRechargeCallback-{$payment}", '余额充值回调-POST参数');
        
        // if($payment == 'Wanmei'){
            // $paycls  = new \payment\Wanmei();
            
            $cls_name = '\payment\\'.$payment;
            $paycls   = new $cls_name();
            $payinfo = $paycls->response();

            if($payinfo && !empty($payinfo['order_sn'])){
                $recharge_info = TradeModel::trade_handle('recharge', ['order_sn'=>$payinfo['order_sn'],'status'=>$payinfo['status'] ]);
                if(!$recharge_info) return $this->error(__('订单不存在或已经支付成功'));
                
                
                //充值成功奖励(首充奖励，二充奖励，推荐奖励，银行充值奖励) ==== Start ====//
                $user_info = Db::name('user')->where("id='{$recharge_info['user_id']}'")->find();
                $user_id = $user_info['id'];
                //发送通知
                \app\common\model\Notice::recharge([ 'user_id'=>$user_id, 'money'=>$recharge_info['money'] ]);

                //发送红利给邀请人
                if($user_info['invite_user_id'] > 1 && ($user_info['invite_user_id'] != $user_id) && $user_info['is_agent'] == 0){
                    BonusLib::invite($user_info['invite_user_id'], $user_id); //邀请送红利
                } 

                /*
                $user_id = $recharge_info['user_id'];
                $money   = $recharge_info['money'];
                $count   = Db::name('trade_recharge')->where("user_id='{$recharge_info['user_id']}' AND status=1")->count();
                if($count == 1){
                    BonusLib::first_recharge($user_id, $money);
                    $invite_user_id = Db::name('user')->where("id='{$user_id}'")->value('invite_user_id');
                    BonusLib::invite($invite_user_id, $money);
                } 
                if($count == 2) BonusLib::second_recharge($user_id, $money);
                $paytype = Db::name('trade_recharge')->where("order_sn='{$recharge_info['order_sn']}'")->value('paytype');
                if( $paytype == 'bank') BonusLib::bank_recharge($user_id, $money);
                */
                //奖励结束 ====== End =======//

                return $this->success(__('ok'));
            }
            return $this->error(__('回调失败'));

        // }

    }

    public function pay_finish(){
        return $this->success(__('付款成功,请留意账户余额'));
    }

    /**
     * 回调测试
     * @param string $type 类型|recharge=充值,cashout=提现,transfer=转账
     * @param string $order_sn 订单号
     * @param string $status   状态
     */
    public function test(){
    	$type = $this->input("type");
    	$order_sn = $this->input("order_sn");
        $status   = $this->input("status");

    	if(!TradeModel::trade_handle($type, ['order_sn'=>$order_sn,'status'=>$status])) return $this->error(__('操作失败,请稍后再试'));
    	return $this->success(__('ok'));
    }




}
